package com.beetlestech.nfe.main.biz.com.impl;

import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.main.biz.com.HbComment;
import com.beetlestech.nfe.main.biz.com.dto.CourseInfoComDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.constant.SQLID;
import com.beetlestech.nfe.main.db.tbl.CourseevaluationDTO;
import com.beetlestech.nfe.main.db.tbl.CourserecommendDTO;
import com.beetlestech.nfe.main.db.tbl.MstcourseDTO;






public class HbCommentImpl extends AbstractDBLogic implements HbComment, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	protected static final String SH_INFO = "课程表";
	//取出所有课程
	@Override
	public CourseInfoComDTO[]  getCommentInfo(){
		//自定义DTO
		List<CourseInfoComDTO> ccDTO=new ArrayList<CourseInfoComDTO>();
		//课程表DTO
		MstcourseDTO mcDTO=new MstcourseDTO();
		mcDTO.setstate(1);
		//取出所有课程的id 和 name
		List<MstcourseDTO> listm = queryDAO.queryForObjectList(MstcourseDTO.EQUAL_SEARCH,mcDTO);
		//循环赋值
		for(int i=0;i<listm.size();i++){
			Integer cid=listm.get(i).getcourseid();
			String cname=listm.get(i).getname();
			CourseInfoComDTO ccdto=new CourseInfoComDTO();
			ccdto.setCourseId(cid);
			ccdto.setName(cname);
			ccDTO.add(ccdto);
		}
		return ccDTO.toArray(new CourseInfoComDTO[listm.size()]);
	}
	
	//取出已推荐课程
	@Override
	public CourseInfoComDTO[] getYesCommentInfo(){
		//自定义DTO
				List<CourseInfoComDTO> ccDTO=new ArrayList<CourseInfoComDTO>();
				//查询出已经被推荐的课程ID
				CourserecommendDTO courseDTO=new CourserecommendDTO();
				courseDTO.set_orderByCondition(CourserecommendDTO.COLUMN_ORDERBY);
				List<CourserecommendDTO> listc = queryDAO.queryForObjectList(CourserecommendDTO.EQUAL_SEARCH,courseDTO);
				List<MstcourseDTO> listm=new ArrayList<MstcourseDTO>();
				for(int i=0;i<listc.size();i++){
					//课程表DTO
					MstcourseDTO mcDTO=new MstcourseDTO();
//					mcDTO.setstate(1);
					mcDTO.setcourseid(listc.get(i).getcourseid());
					
					//取出所有课程的id 和 name
					mcDTO =  queryDAO.queryForObject(MstcourseDTO.EQUAL_SEARCH, mcDTO);
//					if(mcDTO!=null){
						listm.add(mcDTO);	
//					}
					
					
				}
				//循环赋值
				for(int i=0;i<listm.size();i++){
					Integer cid=listm.get(i).getcourseid();
					String cname=listm.get(i).getname();
					CourseInfoComDTO ccdto=new CourseInfoComDTO();
					ccdto.setCourseId(cid);
					ccdto.setName(cname);
					ccDTO.add(ccdto);
				}
					return ccDTO.toArray(new CourseInfoComDTO[listm.size()]);
	}
	
	
	
	//取出未推荐课程
	@Override
	public CourseInfoComDTO[]  getNoCommentInfo(){
		//自定义DTO
		List<CourseInfoComDTO> ccDTO=new ArrayList<CourseInfoComDTO>();
		//查询出已经被推荐的课程ID
		CourserecommendDTO courseDTO=new CourserecommendDTO();
		List<CourserecommendDTO> listc = queryDAO.queryForObjectList(CourserecommendDTO.EQUAL_SEARCH,courseDTO);
	
		if(listc.size()<=0){
			MstcourseDTO mcDTO=new MstcourseDTO();
			List<MstcourseDTO> listm = queryDAO.queryForObjectList(MstcourseDTO.EQUAL_SEARCH,mcDTO);
			for(int i=0;i<listm.size();i++){
				Integer cid=listm.get(i).getcourseid();
				String cname=listm.get(i).getname();
				CourseInfoComDTO ccdto=new CourseInfoComDTO();
				ccdto.setCourseId(cid);
				ccdto.setName(cname);
				ccDTO.add(ccdto);
			}
			return ccDTO.toArray(new CourseInfoComDTO[listm.size()]);
		
		}else{
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<listc.size();i++){
			if(sb.length()>0){
				sb.append(",");
			}
			sb.append(listc.get(i).getcourseid());
		}
		
		//课程表DTO
		MstcourseDTO mcDTO=new MstcourseDTO();
		//自己拼SQL字符串
		mcDTO.set_dynamicWhereCondition
		(MstcourseDTO.COLUMN_COURSE_ID +  " not in (" + sb.toString() + " ) and " + MstcourseDTO.COLUMN_STATE + " = " + CATAGORY_P_COURSE_STATE.PUBLISHED.toString());
		//取出所有课程的id 和 name
		List<MstcourseDTO> listm = queryDAO.queryForObjectList(MstcourseDTO.SQL_SEARCH,mcDTO);
		//循环赋值
				for(int i=0;i<listm.size();i++){
					Integer cid=listm.get(i).getcourseid();
					String cname=listm.get(i).getname();
					CourseInfoComDTO ccdto=new CourseInfoComDTO();
					ccdto.setCourseId(cid);
					ccdto.setName(cname);
					ccDTO.add(ccdto);
				}
		
		
				return ccDTO.toArray(new CourseInfoComDTO[listm.size()]);
		}
	}
	
	
	
	//新增评论
	@Override
	public void SaveComment(Integer stuId,Integer courseId,String content){
		
		CourseevaluationDTO cevDTO=new CourseevaluationDTO();
		cevDTO.setstudentid(stuId);
		cevDTO.setcourseid(courseId);
		cevDTO.setcontent(content);
		//对DB执行操作
		updateDAO.updateOne(CourseevaluationDTO.INSERT, cevDTO, M10190WS, SH_INFO);
	}
	
	//新增推荐课程
	@Override
	public void SavaCourse(Integer[] courseId){
		
		//新增之前先清空表里数据
			CourserecommendDTO courseDTO=new CourserecommendDTO();
			updateDAO.update(SQLID.COURSE_RECOMMEND_DELETE_BY, courseDTO);

		//开始像推荐表里 插入数据
		for(int i=0;i<courseId.length;i++){
			courseDTO.setcourseid(courseId[i]);
			courseDTO.setorderBy(i+1);

			updateDAO.updateOne(CourserecommendDTO.INSERT, courseDTO, M10190WS, SH_INFO);
		}
		
	}
	
}
